package o2o.web.actions.pay;


import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYMathDoubleUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyou.utils.security.EncrypMD5;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.agent.CityAgentMemory;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.order.OrderStatusMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.sys.SysConfigPayMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.cache.user.UserPwdMemory;
import o2o.cache.wallet.UserWalletMemory;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.order.*;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.sys.SysConfigPay;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.PO.wallet.PAY_TYPE;
import o2o.dao.PO.wallet.UserWallet;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.order.OrderMan;
import o2o.web.actions.order.thread.OrderThread;
import o2o.web.actions.user.User;
import o2o.web.actions.wallet.UserWalletMan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pay.ChannelUtils;
import pay.WxAliTriPayUtils;
import pay.alipay.AliPayUtils;
import pay.config.Constant;
import pay.config.WeixinConfig;
import pay.dao.WX_ALI_TRI_CHANNEL_TYPE;
import pay.exceptions.RepeatPayExcetion;
import pay.weixin.WeixinPayUtils;
import wx.utils.WeixinUtil;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashSet;

//支付
@Controller
public class OrderPayAction {


    /**
     * 余额支付
     */
    //余额支付
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/byWallet")
    @ResponseBody
    public String pay_order_wallet(String token, String sysAppKey, String appFlag, String orderNo, String payType, String payPassword) {
        // 判断支付方式是否存在
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("请选择需要支付的订单");
        }

        if (OrderMan.isMultiOrder(orderNo)) {
            return AjaxResult.errorResult("尚未开通余额支付");
        }

        if (OrderMan.isMultipleSonOrder(orderNo)) {
            return AjaxResult.errorResult("尚未开通支付子订单");
        }

        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return AjaxResult.errorResult("订单不存在，请检查订单");
        }

        if (ValidateUtil.isNull(payType)) {
            payType = PAY_TYPE.WALLET;//钱包余额
        }

        if (!PAY_TYPE.WALLET.equalsIgnoreCase(payType)) {
            return AjaxResult.errorResult("请选择钱包余额支付");
        }

        //获取店铺信息
        ShopInfo shopInfo = null;
        //支付时检测店铺是否营业中  1检测  0不检测
        if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {

        } else {
            //跑腿订单
            if (OrderMan.isMultipleParentOrder(orderInfo)) {
                if (ValidateUtil.isNotID(orderInfo.getAgentId())) {
                    return AjaxResult.errorResult("商圈信息不能为空");
                } else {
                    CityAgent cityAgent = CityAgentMemory.getInstance().getById(orderInfo.getAgentId());
                    if (cityAgent == null) {
                        return AjaxResult.errorResult("商圈不存在,请核实");
                    }
                }
            } else {
                if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {//支付订单
                    shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
                    if (null == shopInfo) {
                        return AjaxResult.errorResult("获取店铺信息失败");
                    }
                }
            }
//            if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {//支付订单
//                shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
//                if (null == shopInfo) {
//                    return AjaxResult.errorResult("获取店铺信息失败");
//                }
//            }
        }

        if (null != orderInfo.getIsPaid() && 0 == 1 - orderInfo.getIsPaid()) {
            //已经支付完了
        } else {
            //还没有支付
            // 验证支付密码 yangtk
            if (SYS_SETTINGS.VERIFY_PAYMENT_PASSWORD != null && 0 == 1 - SYS_SETTINGS.VERIFY_PAYMENT_PASSWORD) {
                if (payPassword == null || "".equals(payPassword)) {
                    return AjaxResult.errorResult("支付密码不能为空");
                }

                String getPayPassword = UserPwdMemory.getInstance().getPayPassword(user.getUsername());
                if (payPassword == null || "".equals(payPassword)) {
                    return AjaxResult.errorResult("请先设置独立的支付密码");
                }
                // check
                try {
                    payPassword = EncrypMD5.eccrypt(payPassword);
                } catch (Exception e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("支付失败，请联系管理员");
                }

                if (!getPayPassword.equals(payPassword)) {
                    return AjaxResult.errorResult("支付密码错误，请重新输入");
                }
                // 验证密码成功 ：支付
            }
            // end
            if (orderInfo.getTotalPrice() > 0) {
                UserWallet userWallet = UserWalletMemory.getInstance().getWallet(user.getUsername());
                //是否有钱包
                if (null == userWallet) {
                    return AjaxResult.errorResult("您的余额不足，请选择其他支付方式");
                }
                Double walletMoney = userWallet.getMoney();
                Double walletAwardMoney = userWallet.getAwardMoney();
                Double totalWalletMoney = JYMathDoubleUtils.add(walletMoney, walletAwardMoney);
                //钱包可用余额是否够
                if (JYMathDoubleUtils.sub(totalWalletMoney, orderInfo.getTotalPrice()) < 0) {
                    return AjaxResult.errorResult("您的余额不足，请选择其他支付方式");
                }

                //扣除余额
                ResultInfo resultInfo = UserWalletMan.saveOrUpdateUserWallet(user.getUsername(), user.getUserType(), null, UserWalletMan.PAY, orderInfo.getTotalPrice(), 0d, user.getUsername());
                if (null != resultInfo) {
                    if (!resultInfo.isSuccess()) {
                        return AjaxResult.errorResult(resultInfo.getError());
                    }
                } else {
                    return AjaxResult.errorResult("支付失败，请稍后再试");
                }
//                //添加订单支付流水
//                ResultInfo payResult = OrderPay.addOrderPay(orderNo, payType, null, null, user.getUsername(), orderInfo, shopInfo);// 余额支付 的
//                if (null == payResult) {
//                    //todo 回滚钱包金额
//                    return AjaxResult.errorResult("增加流水失败，请稍后再试。");
//                } else {
//                    if (!resultInfo.isSuccess()) {
//                        //todo 回滚钱包金额
//                        return AjaxResult.errorResult(payResult.getError());
//                    }
//                }

                // 添加钱包支付流水
                ResultInfo userWalletFlowResult = OrderPay.userAddFlow(orderInfo, user.getUsername());
                if (null == userWalletFlowResult) {
                    //todo 回滚钱包金额
                    return AjaxResult.errorResult("添加钱包支付流水时失败，请稍后再试");
                } else {
                    if (!userWalletFlowResult.isSuccess()) {
                        //todo 回滚钱包金额
                        return AjaxResult.errorResult(userWalletFlowResult.getError());
                    }
                }
//                if (OrderMan.isMultipleParentOrder(orderNo)) {
//                    // 聚合訂單
//                    ResultInfo t = OrderPay.modifyMultipleOrderStatus(orderNo);
//                    if (t == null) {
//                        System.out.println("聚合订单修改订单状态失败V1.1");
//                        return AjaxResult.errorResult("更新订单状态失败，请稍后再试V1.1");
//                    }
//                    if (!resultInfo.isSuccess()) {
//                        System.out.println(resultInfo.getError());
//                        return AjaxResult.errorResult(resultInfo.getError());
//                    }
//                }
            }
        }
        //如果是自取的话 生成取货验证码
//        String verifyCode = "";
//        try {
//            if (0 == 1 - orderInfo.getIsZiQu()) {
//                verifyCode = ",verifyCode='" + OrderMan.getVerifyCodeDaodian() + "'";
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            //todo 回滚钱包金额
//        }
//        if (ValidateUtil.isNull(verifyCode)) {
//            verifyCode = "";
//        }

//        //已成功支付，更新订单状态
//        StringBuilder sb = new StringBuilder();
//        sb.append("update ").append(PojoTables.OrderInfo).append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED).append(verifyCode).append(",payType='").append(PAY_TYPE.WALLET)
//                .append("' where delFlag =0 and  orderNo = '").append(orderNo).append("'");
//        try {
//            UserDaoSql.getInstance().executeBySql(sb.toString());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            //todo 回滚钱包金额
//            return AjaxResult.errorResult("更新订单状态失败，请稍后再试");
//        }

//        //增加订单状态记录
//        Long time = DateUtils.getCurrTime();
//        OrderStatus orderStatus = new OrderStatus(orderNo, ORDER_STATUS.PAYED, user.getUsername(), time);
//        try {
//            UserDaoSql.getInstance().insertObj(PojoTables.OrderStatus, orderStatus);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//
//        //更新缓存中订单状态
//        OrderStatusMemory.getInstance().addStatus(orderNo);
//        OrderInfoMemory.getInstance().modifyStatus(orderNo);

//        //跑腿订单
//        if (null != orderInfo.getOrderType() &&
//                (0 == ORDER_TYPE.PAO_TUI - orderInfo.getOrderType()
//                        || 0 == ORDER_TYPE.DAI_GOU - orderInfo.getOrderType()
//                        || 0 == ORDER_TYPE.BANG_BAN - orderInfo.getOrderType()
//                        || 0 == ORDER_TYPE.HUO_YUN - orderInfo.getOrderType())
//                ) {//跑腿订单
//            StringBuilder sbOther = new StringBuilder();
//            sbOther.append("update ").append(PojoTables.OTHER_ORDER_INFO).append(" set isPaid =1,orderStatus=").append(ORDER_STATUS.PAYED)
//                    .append(verifyCode).append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
//            try {
//                UserDaoSql.getInstance().executeBySql(sbOther.toString());
//            } catch (SQLException e) {
//                e.printStackTrace();
//                //todo 回滚支付
//                return AjaxResult.errorResult("更新订单状态失败，请稍后再试");
//            }
//            //缓存修改
//            OtherOrderMemory.getInstance().addOrModify(orderNo);
//        }


//        if (null != shopInfo) {
//            //极光推送
////            JPushMessage jPushMessage = new JPushMessage(shopInfo.getOwnUsername(), "又有新的订单了", "有新的订单，请尽快处理");
////            jPushMessage.setPushUserType(JPushMessage.SHOP_OWNER);//发给店主
////            JPushTask.jPushMessageList.add(jPushMessage);
//            if (OrderMan.isMultipleParentOrder(orderNo)) {
//
//            } else {
//                Notice.publishNotice2Person(sysAppKey, user.getUsername(), JPushMessage.SHOP_OWNER, NoticeType.order, "又有新的订单了", "有新的订单，请尽快处理");
//            }
////            Notice.publishNotice2Person(sysAppKey, user.getUsername(), JPushMessage.SHOP_OWNER, NoticeType.order, "又有新的订单了", "有新的订单，请尽快处理");
//        }


        //更新支付方式
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.OrderInfo)
                .append(" set payType='").append(PAY_TYPE.WALLET)
                .append("',payType='").append(payType).append("',payTime=").append(DateUtils.getCurrTime()).append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("更新支付方式失败，请稍后再试");
        }


        // 聚合订单的话需要更新子订单
        if (OrderMan.isMultipleParentOrder(orderInfo)) {
            //更新支付方式
            StringBuilder sbPayType = new StringBuilder();
            sbPayType.append("update ").append(PojoTables.OrderInfo)
                    .append(" set payType='").append(PAY_TYPE.WALLET)
                    .append("',payType='").append(payType).append("',payTime=").append(DateUtils.getCurrTime()).append(" where delFlag =0 and  parentOrderNo = '").append(orderNo).append("'");
            try {
                UserDaoSql.getInstance().executeBySql(sbPayType.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("更新支付方式失败，请稍后再试");
            }
        }

        ResultInfo resultInfo = OrderPay.userOrderPayOver(sysAppKey, appFlag, payType, orderNo, "");
        if (null == resultInfo) {
            return AjaxResult.errorResult("请稍后重试");
        } else {
            if (!resultInfo.isSuccess()) {//失败，返回错误
                return AjaxResult.errorResult(resultInfo.getError());
            }
        }

        return AjaxResult.successResult();
    }

    /**
     * 货到付款
     */
    //货到付款
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/arrive")
    @ResponseBody
    public String pay_order_arrive(String token, String sysAppKey, String appFlag, String orderNo, String payType) {
        // 用户是否已登录
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("请选择需要支付的订单");
        }

        if (OrderMan.isMultiOrder(orderNo)) {
            HashSet<String> orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    arrivePay(sysAppKey, appFlag, rOrderNo, payType, user.getUsername());
                }
            }
        } else {
            ResultInfo resultInfo = arrivePay(sysAppKey, appFlag, orderNo, payType, user.getUsername());
            if (null == resultInfo) {
                return AjaxResult.errorResult("支付失败，请稍后再试");
            } else {
                if (!resultInfo.isSuccess()) {
                    return AjaxResult.errorResult(resultInfo.getError());
                }
            }
        }


//        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
//        if (null == orderInfo) {
//            return AjaxResult.errorResult("订单不存在，请检查订单");
//        }
//        if (0 == 1 - orderInfo.getIsPaid()) {
//            return AjaxResult.errorResult("订单已提交成功，无需重复提交");
//        }
//
//        if (ValidateUtil.isNull(payType)) {
//            payType = PAY_TYPE.ARRIVE;//钱包余额
//        }
//
//        if (!PAY_TYPE.ARRIVE.equalsIgnoreCase(payType)) {
//            return AjaxResult.errorResult("请选择货到付款");
//        }
//
//        //如果店铺是货到付款
//        if (null != orderInfo.getShopId()) {
//            ShopInfo shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
//            if (null == shopInfo) {
//                return AjaxResult.errorResult("该店铺不支持货到付款");
//            } else {
//                if (null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {//店铺自配送的不可以货到付款
//                    return AjaxResult.errorResult("该店铺不支持货到付款.");
//                }
//            }
//        }
//
//        //已成功支付，更新订单状态
//        StringBuilder sb = new StringBuilder();
//        sb.append("update ").append(PojoTables.OrderInfo).append(" set payType='").append(payType)
//                .append("',updateUser= '").append(user.getUsername()).append("' where delFlag =0 and  orderNo = '").append(orderNo).append("'");
//        try {
//            UserDaoSql.getInstance().executeBySql(sb.toString());
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("更新订单状态失败，请稍后再试");
//        }
//        //更新缓存中订单状态
//        OrderInfoMemory.getInstance().modifyStatus(orderNo);
//
//        ResultInfo resultInfo = OrderPay.orderPayOver(payType, orderNo, "");
//        if (null == resultInfo) {
//            return AjaxResult.errorResult("请稍后重试");
//        } else {
//            if (!resultInfo.isSuccess()) {//失败，返回错误
//                return AjaxResult.errorResult(resultInfo.getError());
//            }
//        }

        return AjaxResult.successResult();
    }

    //货到付款
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/arrive/v2")
    @ResponseBody
    public String pay_order_arrive_V2(String token, String sysAppKey, String appFlag, String orderNo, Integer isArrivePay) {
        System.out.println("货到付款~~~~" + orderNo);
        // 用户是否已登录
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!ValidateUtil.isAbsInteger(isArrivePay)) {
            isArrivePay = 1;
        }

        if (ValidateUtil.isNull(sysAppKey)) {
            return AjaxResult.errorResult("标识不能为空");
        }

        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("请选择需要支付的订单");
        }

        // 检测是是否支持货到付款
        // if (SYS_SETTINGS.IS_CASH_ON_DELIVERY != null && 0 == 1 - SYS_SETTINGS.IS_CASH_ON_DELIVERY) {
        if (SYS_SETTINGS.IS_CASH_ON_DELIVERY != null && (SYS_SETTINGS.IS_CASH_ON_DELIVERY & ORDER_PAY_CASH_TYPE.PAY_CASH_TYPE_PLATE_POST) > 0) {

        } else {
            return AjaxResult.errorResult("平台尚不支持货到付款");
        }


        if (OrderMan.isMultiOrder(orderNo)) {
            HashSet<String> orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    arrivePayV2(sysAppKey, rOrderNo, user.getUsername(), isArrivePay);
                }
            }
        } else {
            OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
            ShopInfo shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
            if (null == shopInfo) {
                return AjaxResult.errorResult("该店铺已不存在 请核实");
            }
            if (shopInfo.getIsCashOnDelivery() == null || 0 == shopInfo.getIsCashOnDelivery()) {
                return AjaxResult.errorResult("该店铺尚不支持货到付款");
            }
            // 订单未空
            if (orderInfo == null) {
                return AjaxResult.errorResult("该订单已不存在");
            }
            // 已经支付？
            if (orderInfo.getIsPaid() != null && 0 == 1 - orderInfo.getIsPaid()) {
                return AjaxResult.errorResult("该订单已经支付过了，无需再次支付");
            }
            // 已经删除
            if (orderInfo.getOrderStatus() != ORDER_STATUS.SUBMIT) {
                return AjaxResult.errorResult("该状态下不准此操作");
            }
            arrivePayV2(sysAppKey, orderNo, user.getUsername(), isArrivePay);
            ResultInfo resultInfo = OrderPay.userOrderPayOver(sysAppKey, appFlag, PAY_TYPE.ARRIVE, orderNo, "");
            if (null == resultInfo) {
                return AjaxResult.errorResult("请稍后重试");
            } else {
                if (!resultInfo.isSuccess()) {//失败，返回错误
                    return AjaxResult.errorResult(resultInfo.getError());
                }
            }
        }
        return AjaxResult.successResult();
    }

    private ResultInfo arrivePayV2(String sysAppKey, String orderNo, String username, Integer isArrivePay) {

        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return new ResultInfo("订单不存在，请检查订单");
        }
        if (0 == 1 - orderInfo.getIsPaid()) {
            return new ResultInfo("订单已提交成功，无需重复支付");
        }
        StringBuffer sql = new StringBuffer();
        sql.append("update ").append(PojoTables.OrderInfo).append(" set isArrivePay =").append(isArrivePay).append(",orderStatus =").append(ORDER_STATUS.ARRIVE_PAY)
                .append(", payType ='").append(PAY_TYPE.ARRIVE).append("'")
                .append(",payTime=").append(DateUtils.getCurrTime())
                .append(",updateUser= '")
                .append(username).append("' where delFlag =0 and  orderNo = '").append(orderNo).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("订单使用货到付款异常");
        }
        // 缓存
        OrderInfoMemory.getInstance().modifyStatus(orderNo);
        // 检测支付时使用
        orderInfo.setIsArrivePay(isArrivePay);
        orderInfo.setOrderStatus(ORDER_STATUS.ARRIVE_PAY);

        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setOrderNo(orderNo);
        orderStatus.setCreateUser(username);
        orderStatus.setOrderStatus(ORDER_STATUS.ARRIVE_PAY);
        orderStatus.setCreateTime(DateUtils.getCurrTime());
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.OrderStatus, orderStatus);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("订单装态更新失败");
        }
        OrderStatusMemory.getInstance().addStatus(orderNo);
        // ShopInfo shopInfo = null;
        // if (ValidateUtil.isID(orderInfo.getShopId())) {
        //     shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
        // }
        // // 极光推送给用户
        // if (null == shopInfo && null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {
        //     JPushMessage jPushMessage = new JPushMessage(true, orderInfo.getShopCity(), "又有新的订单了", "又有新的订单了，快去抢单吧");
        //     jPushMessage.setPushUserType(JPushMessage.SHOP_OWNER);//推给配送员
        //     JPushTask.jPushMessageList.add(jPushMessage);
        // } else {
        //     // 更新装态信息
        //     OrderStatusMemory.getInstance().addStatus(orderNo);
        //     Notice.publishNotice2Person(sysAppKey, shopInfo.getOwnUsername(), JPushMessage.SHOP_OWNER, NoticeType.order, "又有新的订单了", "有新的订单，请尽快处理");
        //     if (null != SYS_SETTINGS.SHOP_NEW_ORDER_SMS && 0 == 1 - SYS_SETTINGS.SHOP_NEW_ORDER_SMS) {
        //         JYSmsSeverBusiness.sendSMS(sysAppKey, shopInfo.getTelephone(), SMS_TYPE.SHOP_NEW_ORDER);
        //     }
        // }
        // 移除取消检测的信息
        if (OrderThread.map.get(orderNo) != null) {
            OrderThread.map.remove(orderNo);
        }
        return new ResultInfo();
    }


    // 线下付款
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/underline")
    @ResponseBody
    public String pay_under_line(String token, String sysAppKey, String appFlag, String orderNo, String payType) {
        // 管理员是否已登录
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        // 只有管理员才能操作
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }
        if (orderNo == null) {
            return AjaxResult.errorResult("请选择操作的订单");
        }
        ResultInfo resultInfo = underlinePay(sysAppKey, appFlag, orderNo, payType, user.getUsername());
        if (!resultInfo.isSuccess()) {
            return AjaxResult.errorResult(resultInfo.getError());
        }
        return AjaxResult.successResult();
    }

    public ResultInfo underlinePay(String sysAppKey, String appFlag, String orderNo, String payType, String opUsername) {
        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (orderInfo == null) {
            return new ResultInfo("该订单已不存在");
        }
        if (ValidateUtil.isNull(payType)) {
            payType = PAY_TYPE.UNDERLINE;// 线下付款
        }
        if (orderInfo.getOrderStatus() != ORDER_STATUS.UNDERLINE_SUBMIT) {
            return new ResultInfo("您只能操作线下支付订单");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.OrderInfo).append(" set payType='").append(payType)
                .append("',updateUser= '").append(opUsername).append("'")
                .append(",payTime=").append(DateUtils.getCurrTime())
                .append(" where delFlag =0 and  orderNo = '").append(orderNo).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("更新订单状态失败，请稍后再试");
        }
        //更新缓存中订单状态
        OrderInfoMemory.getInstance().modifyStatus(orderNo);
        ResultInfo resultInfo = OrderPay.userOrderPayOver(sysAppKey, appFlag, payType, orderNo, "");
        if (null == resultInfo) {
            return new ResultInfo("请稍后重试");
        } else {
            if (!resultInfo.isSuccess()) {//失败，返回错误
                return new ResultInfo(resultInfo.getError());
            }
        }
        return new ResultInfo();
    }


    // 货到付款
    private ResultInfo arrivePay(String sysAppKey, String appFlag, String orderNo, String payType, String opUsername) {
        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
        if (null == orderInfo) {
            return new ResultInfo("订单不存在，请检查订单");
        }
        if (0 == 1 - orderInfo.getIsPaid()) {
            return new ResultInfo("订单已提交成功，无需重复提交");
        }

        if (ValidateUtil.isNull(payType)) {
            payType = PAY_TYPE.ARRIVE;
        }

        if (!PAY_TYPE.ARRIVE.equalsIgnoreCase(payType)) {
            return new ResultInfo("请选择货到付款");
        }

        //如果店铺是货到付款
        if (null != orderInfo.getShopId()) {
            ShopInfo shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
            if (null == shopInfo) {
                return new ResultInfo("该店铺不支持货到付款");
            } else {
                if (null != shopInfo.getIsSelfPost() && 0 == 1 - shopInfo.getIsSelfPost()) {//店铺自配送的不可以货到付款
                    if ((ORDER_PAY_CASH_TYPE.PAY_CASH_TYPE_SELF_POST & SYS_SETTINGS.IS_CASH_ON_DELIVERY) <= 0 || (shopInfo.getIsCashOnDelivery() & ORDER_PAY_CASH_TYPE.PAY_CASH_TYPE_SELF_POST) <= 0) {
                        return new ResultInfo("该店铺不支持货到付款.");
                    }
                }
            }
        }

        //已成功支付，更新订单状态
        StringBuilder sb = new StringBuilder();
//        sql.append("update ").append(PojoTables.OrderInfo).append(" set isArrivePay =").append(isArrivePay).append(",orderStatus =").append(ORDER_STATUS.ARRIVE_PAY)
//                .append(",updateUser= '")
//                .append(username).append("' where delFlag =0 and  orderNo = '").append(orderNo).append("'");

        sb.append("update ").append(PojoTables.OrderInfo).append(" set payType='").append(payType).append("'")
                .append(",payTime = ").append(DateUtils.getCurrTime())
                .append(",isArrivePay=").append(1).append(",orderStatus =").append(ORDER_STATUS.ARRIVE_PAY)
                .append(",updateUser= '").append(opUsername).append("' where delFlag =0 and  orderNo = '").append(orderNo).append("'");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return new ResultInfo("更新订单状态失败，请稍后再试");
        }
        //更新缓存中订单状态
        OrderInfoMemory.getInstance().modifyStatus(orderNo);

        ResultInfo resultInfo = OrderPay.userOrderPayOver(sysAppKey, appFlag, payType, orderNo, "");
        if (null == resultInfo) {
            return new ResultInfo("请稍后重试");
        } else {
            if (!resultInfo.isSuccess()) {//失败，返回错误
                return new ResultInfo(resultInfo.getError());
            }
        }

        return new ResultInfo();
    }


    /**
     * ------------------第三方支付
     */
    //获取签名
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/sign")
    @ResponseBody
    public String order_pay_getSign(String token, String sysAppKey, String appFlag,// 项目子标识
                                    String orderNo, String payType, Integer userType, Long integral,
                                    String wxXcxOpenId,
                                    String purchaserId) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("订单编号不能为空");
        }

        if (ValidateUtil.isNull(payType)) {
            return AjaxResult.errorResult("支付方式不能为空");
        }
        boolean isMultiOrder = false;
        if (OrderMan.isMultiOrder(orderNo)) { //是否为聚合订单
            isMultiOrder = true;
        }

        OrderInfo orderInfo = null;
        Double totalMoney = 0d;//需要支付的金额
        orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);

        if (null == orderInfo) {
            return AjaxResult.errorResult("订单不存在");
        }
        // 判断数据是不是会员开通的
//        if (SYS_SETTINGS.HAS_MEMBER_RECHARGE == 1) {
//          不在判断开通几次问题
//            if (ValidateUtil.isID(orderInfo.getVipPacketGameId()) && user.getVipExpireTime() != null && user.getVipExpireTime() > DateUtils.getCurrTime()) {
//                return AjaxResult.errorResult("您已经开通了vip无法重复开通");
//            }
//        }
        if (orderInfo.getOrderStatus() != ORDER_STATUS.SUBMIT) {
            return AjaxResult.errorResult("订单非提交状态,无法获取签名");
        }
        ShopInfo shopInfo = null;
        HashSet<String> orderSet = null;
        if (orderInfo != null && OrderMan.isMultipleSonOrder(orderInfo)) {
            // 聚合订单
            return AjaxResult.errorResult("你所选择是子订单编号");
        } else if (orderInfo != null && OrderMan.isMultipleParentOrder(orderInfo)) {
            orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String sOrderNo : orderSet) {
                    if (ValidateUtil.isNull(sOrderNo) || OrderMan.isMultipleParentOrder(sOrderNo)) {
                        continue;
                    }
                    OrderInfo sOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(sOrderNo);
                    if (null == sOrderInfo) {
                        return AjaxResult.errorResult("订单信息有误,请核实");
                    }
                    if (null != sOrderInfo.getIsPaid() && 0 == sOrderInfo.getIsPaid() - 1) {//已经支付过了
                        return AjaxResult.errorResult("获取签名错误，请重试V1");
                    }
                    //支付时检测店铺是否营业中  1检测  0不检测
                    if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {

                    } else {
                        if (null != sOrderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - sOrderInfo.getOrderType()) { //外卖订单
                            //获取店铺信息  该店是否在营业
                            shopInfo = ShopMemory.getInstance().getShopById(sOrderInfo.getShopId());
                            if (null == shopInfo) {
                                return AjaxResult.errorResult("当前店铺已不存在，无法继续支付");
                            }
                            if (0 == shopInfo.getIsWork()) {
                                if (null != sOrderInfo.getIsAppointment() && 0 == 1 - sOrderInfo.getIsAppointment()
                                        && 0 == 1 - shopInfo.getIsAppointment()) {
                                    //预约订单不需要检查店铺是否已打烊
                                } else {
                                    return AjaxResult.errorResult("这个店铺对你打了烊");
                                }
                            }
                        }
                    }
                }
            } else {
                return AjaxResult.errorResult("该订单不存在");
            }
            totalMoney = orderInfo.getTotalPrice();
        } else if (isMultiOrder) { //多个订单同时支付
            orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    OrderInfo tOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(rOrderNo);
                    if (null == tOrderInfo) {
                        continue;
                    }
                    if (null != tOrderInfo.getIsPaid() && 0 == tOrderInfo.getIsPaid() - 1) {//已经支付过了
                        continue;
                    }
                    //支付时检测店铺是否营业中  1检测  0不检测
                    if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {

                    } else {
                        if (null != tOrderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - tOrderInfo.getOrderType()) {//跑腿订单
                            //获取店铺信息  该店是否在营业
                            shopInfo = ShopMemory.getInstance().getShopById(tOrderInfo.getShopId());
                            if (null == shopInfo) {
                                return AjaxResult.errorResult("当前店铺已不存在，无法继续支付");
                            }
                            if (0 == shopInfo.getIsWork()) {
                                if (null != tOrderInfo.getIsAppointment() && 0 == 1 - tOrderInfo.getIsAppointment()
                                        && 0 == 1 - shopInfo.getIsAppointment()) {
                                    //预约订单不需要检查店铺是否已打烊
                                } else {
                                    return AjaxResult.errorResult("店铺已打烊");
                                }
                            }
                        }
                    }
                    if (ValidateUtil.isNotAbsDouble(tOrderInfo.getTotalPrice())) {
                        continue;
                    }
                    totalMoney = JYMathDoubleUtils.add(totalMoney, tOrderInfo.getTotalPrice());
                }
            } else {
                return AjaxResult.errorResult("该订单不存在");
            }
        } else { //单订单
            //获取订单信息
            orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
            if (null == orderInfo) {
                return AjaxResult.errorResult("该订单不存在");
            }

            totalMoney = orderInfo.getTotalPrice();

            if (ValidateUtil.isAbsInteger(orderInfo.getIsPaid()) && 0 == orderInfo.getIsPaid() - 1) {//已经支付过了
                return AjaxResult.errorResult("该订单已经支付过了，无需再次支付");
            }
            //支付时检测店铺是否营业中  1检测  0不检测
            if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {

            } else {
                if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {//跑腿订单
                    //获取店铺信息  该店是否在营业
                    shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
                    if (null == shopInfo) {
                        return AjaxResult.errorResult("当前店铺已不存在，无法继续支付");
                    }
                    if (0 == shopInfo.getIsWork()) {
                        if (null != orderInfo.getIsAppointment() && 0 == 1 - orderInfo.getIsAppointment()
                                && 0 == 1 - shopInfo.getIsAppointment()) {
                            //预约订单不需要检查店铺是否已打烊
                        } else {
                            return AjaxResult.errorResult("店铺已打烊");
                        }
//                        return AjaxResult.errorResult("店铺已打烊");
                    }
                }
            }
        }


        //查询店铺是否已支付成功，如果已支付成功，则不需要再支付
        boolean hasPay = OrderPay.hasPaySuccess(sysAppKey, orderInfo.getAgentId(), appFlag, payType, userType, orderNo, orderInfo.getChargeId());
        if (hasPay) {
            return AjaxResult.errorResult("您已支付成功无需重复支付");
        }

        Double integralOffsetMoney = 0.0d;

        Double originalMoney = totalMoney;
        String currencyStr = "";
        //货币金额转换
        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY) {//是否使用主币支付 1使用主币支付 0使用外币支付
            //外币支付需要转换
            //外币汇率
            if (ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {
                //外币计算
                totalMoney = JYMathDoubleUtils.scale(JYMathDoubleUtils.mul(totalMoney, SYS_SETTINGS.EXCHANGE_RATE));

                currencyStr = ",fcTotalPrice=" + totalMoney + ",currencyUnit='" + SYS_SETTINGS.CURRENCY_UNIT + "',exchangeRate=" + SYS_SETTINGS.EXCHANGE_RATE;
            }
        }
        String sign = null;
        String chargeId = "";
        switch (payType) {
            case ChannelUtils.alipay: {//支付宝
                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(orderInfo.getAgentId(), appFlag);
                if (null != sysConfigPay) {
                    // 有商圈配置信息
                    String subject = sysConfigPay.getPaySubject();
                    String content = sysConfigPay.getPayContent();
                    sign = AliPayUtils.getInstance().app_pay(orderNo, Float.parseFloat(totalMoney + ""), "30m", subject, content,
                            sysConfigPay.getAliAppId(), sysConfigPay.getAliPrivateKey(),
                            sysConfigPay.getAliPublicKey(), sysConfigPay.getAliSignType(), sysConfigPay.getAliNotifyUrl());
                } else {
//                    sign = AliPayUtils.getInstance().app_pay(orderNo, Float.parseFloat(totalMoney + ""), "30m", Constant.PAY_SUBJECT, Constant.PAY_CONTENT);

                    //访问支付宝--获取订单状态
//                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 == 1 - SYS_SETTINGS.WX_ALI_USE_TRI) {
                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
//                        String payNo = orderNo + "-" + SysCommon.getRandomString(3);
//                        chargeId = ",chargeId= '" + payNo + "'";
//                        ResultInfo resultInfo = SynoPayUtils.getSign(payNo, totalMoney, "Alipay");

                        ResultInfo resultInfo = WxAliTriPayUtils.triGetPaySign(orderNo, payType, totalMoney, originalMoney);
                        if (null == resultInfo) {
                            System.out.println("获取签名失败 SynoPay");
                            sign = null;
                        } else {
                            if (resultInfo.isSuccess()) {
                                // 获取成功
                                sign = resultInfo.getObj().toString();
                                chargeId = ",chargeId= '" + resultInfo.getMessage() + "'";
                            } else {
                                System.out.println("获取签名失败：" + resultInfo.getError());
                                sign = null;
                            }
                        }
                    } else {
                        sign = AliPayUtils.getInstance().app_pay(orderNo, Float.parseFloat(totalMoney + ""), "30m", Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                    }
                }
            }
            break;
            case ChannelUtils.wx: {//微信
                if (null != userType) {
                    switch (userType) {
                        case UserType.SHOP_OWNER: {// 店铺端
                            try {
                                sign = WeixinPayUtils.getInstance().shop_mobile_direct_pay(orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult(e.getMessage());
                            } catch (RepeatPayExcetion repeatPayExcetion) {
                                repeatPayExcetion.printStackTrace();
                                return AjaxResult.errorResult(repeatPayExcetion.getMessage());
                            }
                        }
                        break;
                        case UserType.POSTMAN: {// 骑手端
                            try {
                                sign = WeixinPayUtils.getInstance().postman_mobile_direct_pay(orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult(e.getMessage());
                            } catch (RepeatPayExcetion repeatPayExcetion) {
                                repeatPayExcetion.printStackTrace();
                                return AjaxResult.errorResult(repeatPayExcetion.getMessage());
                            }
                        }
                        break;
                        default: {
                            try {
                                sign = WeixinPayUtils.getInstance().mobile_direct_pay(orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult(e.getMessage());
                            } catch (RepeatPayExcetion repeatPayExcetion) {
                                repeatPayExcetion.printStackTrace();
                                if (SYS_SETTINGS.HAS_AGENT_PAY == 1) {
                                    return AjaxResult.errorResult("当前订单已有人正在支付");
                                } else {
                                    return AjaxResult.errorResult(repeatPayExcetion.getMessage());
                                }
//                                return AjaxResult.errorResult(repeatPayExcetion.getMessage());
                            }
                        }
                        break;
                    }

                } else {
                    SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(orderInfo.getAgentId(), appFlag);
                    if (null != sysConfigPay) {
                        // 有商圈配置信息
                        String subject = sysConfigPay.getPaySubject();
                        String content = sysConfigPay.getPayContent();
                        String appId = sysConfigPay.getWxAppId();
                        String mchId = sysConfigPay.getWxMchId();
                        String apiKey = sysConfigPay.getWxApiKey();
                        String notify_url = sysConfigPay.getWxNotifyUrl();
                        try {
                            sign = WeixinPayUtils.getInstance().mobile_direct_pay(appId, mchId, apiKey, notify_url, orderNo, totalMoney, 30, subject, content);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                            return AjaxResult.errorResult(e.getMessage());
                        } catch (RepeatPayExcetion repeatPayExcetion) {
                            repeatPayExcetion.printStackTrace();
                        }
                    } else {
                        if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
                            ResultInfo resultInfo = WxAliTriPayUtils.triGetPaySign(orderNo, payType, totalMoney, originalMoney);
                            if (null == resultInfo) {
                                System.out.println("获取签名失败 SynoPay wx");
                                sign = null;
                            } else {
                                if (resultInfo.isSuccess()) {
                                    // 获取成功
                                    sign = resultInfo.getObj().toString();
                                    chargeId = ",chargeId= '" + resultInfo.getMessage() + "'";
                                } else {
                                    System.out.println("获取签名失败：" + resultInfo.getError());
                                    sign = null;
                                }
                            }
                        } else {
                            try {
                                sign = WeixinPayUtils.getInstance().mobile_direct_pay(orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                return AjaxResult.errorResult(e.getMessage());
                            } catch (RepeatPayExcetion repeatPayExcetion) {
                                repeatPayExcetion.printStackTrace();
                                return AjaxResult.errorResult(repeatPayExcetion.getMessage());
                            }
                        }
                    }
                }

            }
            break;

            case ChannelUtils.wx_xcx: { //微信小程序支付
                //获取用户的openid
                if (ValidateUtil.isNull(wxXcxOpenId)) {
                    wxXcxOpenId = user.getWxXcxOpenId();
                    if (ValidateUtil.isNull(wxXcxOpenId)) {
                        return AjaxResult.errorResult("未取得该用户的微信openId无法支付");
                    }
                }
                SysConfigPay sysConfigPay = SysConfigPayMemory.getInstance().getByAgentId(orderInfo.getAgentId(), appFlag);
                if (null != sysConfigPay) {
                    String xcxAppId = sysConfigPay.getWxXcxAppId();
                    String xcxMchId = sysConfigPay.getWxXcxMchId();
                    String xcxNotifyUrl = sysConfigPay.getWxXcxApiNotifyUrl();
                    String xcxApiKey = sysConfigPay.getWxXcxApiKey();
                    sign = WeixinPayUtils.getInstance().winxin_pay_xcx(wxXcxOpenId, orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT,
                            xcxAppId, xcxMchId, xcxApiKey, xcxNotifyUrl);
                } else {
                    if (null != SYS_SETTINGS.WX_ALI_USE_TRI && 0 != WX_ALI_TRI_CHANNEL_TYPE.ORIGINAL - SYS_SETTINGS.WX_ALI_USE_TRI) {
                        ResultInfo resultInfo = WxAliTriPayUtils.triGetXcxPaySign(sysAppKey, appFlag, orderNo, totalMoney, originalMoney, wxXcxOpenId);
                        if (null == resultInfo) {
                            System.out.println("获取签名失败 SynoPay xcx");
                            sign = null;
                        } else {
                            if (resultInfo.isSuccess()) {
                                // 获取成功
                                if (null == resultInfo.getObj()) {
                                    sign = null;
                                } else {
                                    sign = resultInfo.getObj().toString();
                                }
                                chargeId = ",chargeId= '" + resultInfo.getMessage() + "'";
                            } else {
                                System.out.println("获取签名失败：" + resultInfo.getError());
                                sign = null;
                            }
                        }
                    } else {
                        sign = WeixinPayUtils.getInstance().winxin_pay_xcx(sysAppKey, appFlag, wxXcxOpenId, orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                    }
                }
            }
            break;

        }

        if (null == sign) {
            return AjaxResult.errorResult("获取签名失败，请稍后再试");
        }

        String payTypeFlag = "";// 支付渠道，项目子编号
        if (ValidateUtil.isNotNull(appFlag)) {
            payTypeFlag = ",payTypeFlag='" + appFlag + "'";
        }
        String upOrder = "  and orderNo='" + orderNo + "'";
        if (OrderMan.isMultipleParentOrder(orderInfo)) {
            if (orderSet != null && orderSet.size() > 0) {
                for (String tempOrderNo : orderSet) {
                    upOrder = "  and orderNo='" + orderNo + "'";
                    OrderInfo sOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(tempOrderNo);
                    //货币金额转换
                    if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY) {//是否使用主币支付 1使用主币支付 0使用外币支付
                        //外币支付需要转换
                        //外币汇率
                        if (ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {
                            //外币计算
                            Double tempTotalMoney = sOrderInfo.getTotalPrice();
                            tempTotalMoney = JYMathDoubleUtils.scale(JYMathDoubleUtils.mul(tempTotalMoney, SYS_SETTINGS.EXCHANGE_RATE));
                            currencyStr = ",fcTotalPrice=" + tempTotalMoney + ",currencyUnit='" + SYS_SETTINGS.CURRENCY_UNIT + "',exchangeRate=" + SYS_SETTINGS.EXCHANGE_RATE;
                        }
                    }
                    StringBuffer sql = new StringBuffer("update ").append(PojoTables.OrderInfo)
                            .append(" set paySign='" + sign + "'")
                            .append(",payType='" + payType + "'")
                            .append(currencyStr)//货币单位及汇率
                            .append(",payTime=" + DateUtils.getCurrTime())
                            .append(" where delFlag=0 and isPaid=0 ").append(upOrder);
                    try {
                        UserDaoSql.getInstance().executeBySql(sql.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("获取支付签名失败，请稍后再试");
                    }
                }
            }
            // 聚合订单的话需要更新子订单
            //更新支付方式
            StringBuffer sbPayType = new StringBuffer("update ").append(PojoTables.OrderInfo)
                    .append(" set paySign='" + sign + "'")
                    .append(",payType='" + payType + "'")
                    .append(currencyStr)//货币单位及汇率
                    .append(chargeId)// 沃支付，保存交易单号
                    .append(payTypeFlag)//  支付渠道，项目子编号
                    .append(",payTime=" + DateUtils.getCurrTime())
                    .append(" where delFlag =0  and isPaid=0 and parentOrderNo = '").append(orderNo).append("'");
            try {
                UserDaoSql.getInstance().executeBySql(sbPayType.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("更新支付方式失败，请稍后再试");
            }
        } else {
            if (isMultiOrder) {
                upOrder = " and orderNo='" + orderNo + "'";
            }
            // 是不是有代付的功能
            String rePayUsername = "";
            if (SYS_SETTINGS.HAS_AGENT_PAY == 1) { // 是都有代付的功能
                if (!user.getUsername().equals(orderInfo.getUsername())) {
                    rePayUsername = ",rePayUsername ='" + user.getUsername() + "'";
                } else {
                    rePayUsername = ",rePayUsername = ''";
                }
            }
            StringBuffer sql = new StringBuffer("update ").append(PojoTables.OrderInfo)
                    .append(" set paySign='" + sign + "'")
                    .append(",payType='" + payType + "'")
                    .append(currencyStr)//货币单位及汇率
                    .append(rePayUsername) // 代付的人
                    .append(chargeId)// 沃支付，保存交易单号
                    .append(payTypeFlag)// 支付渠道，项目子编号
                    .append(",payTime=" + DateUtils.getCurrTime())
                    .append(" where delFlag=0 and isPaid=0 ").append(upOrder);
            try {
                UserDaoSql.getInstance().executeBySql(sql.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("获取支付签名失败，请稍后再试");
            }
        }
        // 修改订单缓存
        OrderInfoMemory.getInstance().modifyStatus(orderNo);

        // 聚合订单 多商户提交订单
        if (OrderMan.isMultipleParentOrder(orderInfo)) {
            orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    OrderInfo tOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(rOrderNo);
                    if (null == tOrderInfo) {
                        continue;
                    }
                    if (null != tOrderInfo.getIsPaid() && 0 == tOrderInfo.getIsPaid() - 1) {//已经支付过了
                        continue;
                    }
                    tOrderInfo.setPayType(payType);
                    // 只有父订单 才会进入支付检测
                    if (OrderMan.isMultipleParentOrder(tOrderInfo)) {
                        PayThreads.map.put(tOrderInfo.getOrderNo(), tOrderInfo);
                    }
                }
            }
        } else if (isMultiOrder) {
            orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    OrderInfo tOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(rOrderNo);
                    if (null == tOrderInfo) {
                        continue;
                    }
                    if (null != tOrderInfo.getIsPaid() && 0 == tOrderInfo.getIsPaid() - 1) {//已经支付过了
                        continue;
                    }
                    tOrderInfo.setPayType(payType);
                    PayThreads.map.put(tOrderInfo.getOrderNo(), tOrderInfo);

                }
            }
        } else {//单订单
            if (null != orderInfo) {
                orderInfo.setPayType(payType);
                PayThreads.map.put(orderNo, orderInfo);
            }
        }

        // 沃支付返回正常格式
        if (ChannelUtils.wo_pay.equalsIgnoreCase(payType)) {
            return AjaxResult.successResult();
        }
        return sign;
    }

    //获取签名 骑手缴纳押金支付
    @RequestMapping(method = RequestMethod.POST, value = "/postman/pay/sign")
    @ResponseBody
    public String room_payment_order_postman(String token, String sysAppKey, String orderNo, String payType, String
            wxXcxOpenId) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("订单编号不能为空");
        }

        if (ValidateUtil.isNull(payType)) {
            return AjaxResult.errorResult("支付方式不能为空");
        }
        boolean isMultiOrder = false;
        if (OrderMan.isMultiOrder(orderNo)) {//是否为聚合订单
            isMultiOrder = true;
        }

        OrderInfo orderInfo = null;
        Double totalMoney = 0d;//需要支付的金额

        if (isMultiOrder) { //多个订单同时支付
            HashSet<String> orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    OrderInfo tOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(rOrderNo);
                    if (null == tOrderInfo) {
                        continue;
                    }
                    if (null != tOrderInfo.getIsPaid() && 0 == tOrderInfo.getIsPaid() - 1) {//已经支付过了
                        continue;
                    }
                    //支付时检测店铺是否营业中  1检测  0不检测
                    if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {

                    } else {
                        if (null != tOrderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - tOrderInfo.getOrderType()) {//跑腿订单
                            //获取店铺信息  该店是否在营业
                            ShopInfo shopInfo = ShopMemory.getInstance().getShopById(tOrderInfo.getShopId());
                            if (null == shopInfo) {
                                return AjaxResult.errorResult("当前店铺已不存在，无法继续支付");
                            }
                            if (0 == shopInfo.getIsWork()) {
                                if (null != tOrderInfo.getIsAppointment() && 0 == 1 - tOrderInfo.getIsAppointment()
                                        && 0 == 1 - shopInfo.getIsAppointment()) {
                                    //预约订单不需要检查店铺是否已打烊
                                } else {
                                    return AjaxResult.errorResult("店铺已打烊");
                                }
                            }
                        }
                    }

                    if (ValidateUtil.isNotAbsDouble(tOrderInfo.getTotalPrice())) {
                        continue;
                    }
                    totalMoney = JYMathDoubleUtils.add(totalMoney, tOrderInfo.getTotalPrice());
                }
            } else {
                return AjaxResult.errorResult("该订单不存在");
            }
        } else {
            //单订单
            //获取订单信息
            orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
            if (null == orderInfo) {
                return AjaxResult.errorResult("该订单不存在");
            }

            totalMoney = orderInfo.getTotalPrice();

            if (ValidateUtil.isAbsInteger(orderInfo.getIsPaid()) && 0 == orderInfo.getIsPaid() - 1) {//已经支付过了
                return AjaxResult.errorResult("该订单已经支付过了，无需再次支付");
            }
            //支付时检测店铺是否营业中  1检测  0不检测
            if (null != SYS_SETTINGS.PAY_CHECK_SHOP_STATUS && 0 == SYS_SETTINGS.PAY_CHECK_SHOP_STATUS) {

            } else {
                if (null != orderInfo.getOrderType() && 0 == ORDER_TYPE.SHOP_GOODS - orderInfo.getOrderType()) {//跑腿订单
                    //获取店铺信息  该店是否在营业
                    ShopInfo shopInfo = ShopMemory.getInstance().getShopById(orderInfo.getShopId());
                    if (null == shopInfo) {
                        return AjaxResult.errorResult("当前店铺已不存在，无法继续支付");
                    }
                    if (0 == shopInfo.getIsWork()) {
                        if (null != orderInfo.getIsAppointment() && 0 == 1 - orderInfo.getIsAppointment()
                                && 0 == 1 - shopInfo.getIsAppointment()) {
                            //预约订单不需要检查店铺是否已打烊
                        } else {
                            return AjaxResult.errorResult("店铺已打烊");
                        }
                    }
                }
            }
        }

        // 查询是否已经支付成功了
        boolean hasPay = OrderPay.qsHasPaySuccess(sysAppKey, payType, orderNo, "");
        if (hasPay) {
            return AjaxResult.errorResult("您已支付成功无需重复支付");
        }


        String currencyStr = "";
        //货币金额转换
        if (null != SYS_SETTINGS.USE_MAIN_CURRENCY_PAY && 0 == SYS_SETTINGS.USE_MAIN_CURRENCY_PAY) {//是否使用主币支付 1使用主币支付 0使用外币支付
            //外币支付需要转换
            //外币汇率
            if (ValidateUtil.isAbsDouble(SYS_SETTINGS.EXCHANGE_RATE)) {
                //外币计算
                totalMoney = JYMathDoubleUtils.scale(JYMathDoubleUtils.mul(totalMoney, SYS_SETTINGS.EXCHANGE_RATE));

                currencyStr = ",fcTotalPrice=" + totalMoney + ",currencyUnit='" + SYS_SETTINGS.CURRENCY_UNIT + "',exchangeRate=" + SYS_SETTINGS.EXCHANGE_RATE;
            }
        }

        String sign = null;
        switch (payType) {
            case ChannelUtils.alipay: {//支付宝
                sign = AliPayUtils.getInstance().app_pay(orderNo, Float.parseFloat(totalMoney + ""), "30m", Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
            }
            break;
            case ChannelUtils.wx: {//微信
                try {
                    sign = WeixinPayUtils.getInstance().postman_mobile_direct_pay(orderNo, totalMoney, 30, Constant.PAY_SUBJECT, Constant.PAY_CONTENT);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult(e.getMessage());
                } catch (RepeatPayExcetion repeatPayExcetion) {
                    repeatPayExcetion.printStackTrace();
                    return AjaxResult.errorResult(repeatPayExcetion.getMessage());
                }
            }
            break;
        }

        if (null == sign) {
            return AjaxResult.errorResult("获取签名失败，请稍后再试");
        }

        String upOrder = "  and orderNo='" + orderNo + "'";
        if (isMultiOrder) {
            upOrder = " and orderNo='" + orderNo + "'";
        }

        System.out.println("sign:" + sign);
        StringBuffer sql = new StringBuffer("update ").append(PojoTables.OrderInfo)
                .append(" set paySign='" + sign + "'")
                .append(",payType='" + payType + "'")
                .append(currencyStr)//货币单位及汇率
                .append(",payTime=" + DateUtils.getCurrTime())
                .append(" where delFlag=0 and isPaid=0 ").append(upOrder);
        try {
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("获取支付签名失败，请稍后再试");
        }


        if (isMultiOrder) {
            HashSet<String> orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String rOrderNo : orderSet) {
                    if (ValidateUtil.isNull(rOrderNo)) {
                        continue;
                    }
                    OrderInfo tOrderInfo = OrderInfoMemory.getInstance().getOrderInfo(rOrderNo);
                    if (null == tOrderInfo) {
                        continue;
                    }
                    if (null != tOrderInfo.getIsPaid() && 0 == tOrderInfo.getIsPaid() - 1) {//已经支付过了
                        continue;
                    }
                    tOrderInfo.setPayType(payType);
                    PayThreads.map.put(tOrderInfo.getOrderNo(), tOrderInfo);

                }
            }
        } else {//单订单
            if (null != orderInfo) {
                orderInfo.setPayType(payType);
                PayThreads.map.put(orderNo, orderInfo);
            }
        }
        return sign;
    }


    //支付成功调用接口
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/callback")
    @ResponseBody
    public String room_payment_order_finish(String token, String sysAppKey, String appFlag, String orderNo, String payType, Integer userType) {
        // 判断支付方式是否存在
        String username = UserOnlineImpl.getInstance().get(token);
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(username);
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("订单编号不能为空");
        }

        if (ValidateUtil.isNull(payType)) {
            return AjaxResult.errorResult("支付方式不能为空");
        }

        if (ChannelUtils.paypal.equals(payType)) {
            // paypal的不用回调
            return AjaxResult.successResult();
        }

        if (null == userType) {
            userType = UserType.USER;
        }
        if (OrderMan.isMultiOrder(orderNo)) {
            // 多商家订单
            HashSet<String> orderSet = OrderInfoMemory.getInstance().getOrderNoByPayNo(orderNo);
            if (null != orderSet && orderSet.size() > 0) {
                for (String oNo : orderSet) {
                    if (ValidateUtil.isNull(oNo)) {
                        continue;
                    }
                    ResultInfo resultInfo = OrderPay.userOrderPayOver(sysAppKey, appFlag, payType, oNo, null, orderNo, true);
                    if (null == resultInfo) {
                        return AjaxResult.errorResult("请稍后重试");
                    } else {
                        if (!resultInfo.isSuccess()) {//失败，返回错误
                            return AjaxResult.errorResult(resultInfo.getError());
                        }
                    }
                }
            }
        } else {
            // 普通订单
            ResultInfo resultInfo = OrderPay.orderPayOver(sysAppKey, appFlag,
                    payType, userType, orderNo, null, orderNo, true);
            if (null == resultInfo) {
                return AjaxResult.errorResult("请稍后重试");
            } else {
                if (!resultInfo.isSuccess()) {//失败，返回错误
                    return AjaxResult.errorResult(resultInfo.getError());
                }
            }
        }
        return AjaxResult.successResult();
    }



    //查询支付结果
    @RequestMapping(method = RequestMethod.POST, value = "/order/pay/result/get")
    @ResponseBody
    public String payment_result_get(String token, String sysAppKey, String appFlag, String orderNo, String payType) {
        if (ValidateUtil.isNull(orderNo)) {
            return AjaxResult.errorResult("订单编号不能为空");
        }

        if (!OrderMan.isMultiOrder(orderNo)) {
            //获取订单信息
            OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
            if (null == orderInfo) {
                return AjaxResult.errorResult("该订单不存在");
            }

            if (ValidateUtil.isAbsInteger(orderInfo.getIsPaid()) && 0 == orderInfo.getIsPaid() - 1) {//已经支付过了
                return AjaxResult.successResult();
            }
        }

        if (ValidateUtil.isNull(payType)) {
            return AjaxResult.errorResult("支付方式不能为空");
        }

        //查询店铺是否已支付成功，如果已支付成功，则不需要再支付
        boolean hasPay = OrderPay.hasPaySuccess(sysAppKey, appFlag, payType, orderNo, "");
        if (!hasPay) {
            return AjaxResult.errorResult("未支付成功");
        }

        return AjaxResult.successResult();
    }


    //微信客户端访问微信 返回用户code后 调用次接口 保存用户的openId
    //微信code回调
    @RequestMapping(method = RequestMethod.GET, value = "/wx/code/response", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getCodeResponse(String sysAppKey, String appFlag, String code, String state) {
        System.out.println("微信code回调--code:" + code + ",state:" + state);

        String username = state;

//        //boss数据通
//        String appId = "wx0a3fc42a0de29fa2";
//        String appSecret = "7c72514c9aab0e8d701189b8698a5d22";

        if (ValidateUtil.isNull(username)) {
            return AjaxResult.errorResult("获取code失败");
        }

        String gzhAppId = WeixinConfig.gzhAppId;
        String gzhAppSecret = WeixinConfig.gzhAppSecret;
        if (ValidateUtil.isNotNull(appFlag)) {
            SysConfigPay sysConfigPay2 = SysConfigPayMemory.getInstance().getByKey(sysAppKey, appFlag);
            if (null != sysConfigPay2) {
                gzhAppId = sysConfigPay2.getWxGzhAppId();
                gzhAppSecret = sysConfigPay2.getWxGzhAppSecret();
            }
        }
        //获取openid
        String openId = WeixinUtil.getUserOpenID(gzhAppId, gzhAppSecret, code);
        System.out.println(state + "：用户的openId为：" + openId);

        if (ValidateUtil.isNull(openId)) {
            return AjaxResult.errorResult("未获取到用户的openId");
        }

        boolean updateOpenId = true;
        UserInfo userInfo = null;
        if ("snsapi_base".equalsIgnoreCase(username)) {
            //非账号登录
            userInfo = UserInfoMemory.getInstance().getUserInfoByOpenId(openId);
        } else {
            userInfo = UserInfoMemory.getInstance().getUserInfo(username);
        }
        if (null == userInfo) {
            userInfo = UserInfoMemory.getInstance().getUserInfoByOpenId(openId);
            if (null == userInfo) {
                //不存在该用户 注册用户
                boolean regSuccess = false;
                try {
                    regSuccess = User.addUser(openId, "123456", UserType.USER);
                    userInfo = UserInfoMemory.getInstance().getUserInfo(openId);
                } catch (Exception e) {
                    e.printStackTrace();
//                    return AjaxResult.errorResult("根据微信openId注册用户失败");
                    userInfo = UserInfoMemory.getInstance().getUserInfo(openId);
                    if (null != userInfo) {
                        regSuccess = true;
                    } else {
                        return AjaxResult.errorResult("根据微信openId注册用户失败");
                    }
                }
                if (!regSuccess) {
                    return AjaxResult.errorResult("根据微信openId注册用户失败，请重试");
                }

                if (null == userInfo) {
                    userInfo = UserInfoMemory.getInstance().getUserInfoByOpenId(openId);
                }
            } else {
                updateOpenId = false;
            }
        } else {
            if (openId.equalsIgnoreCase(userInfo.getWxGzhOpenId())) {
                updateOpenId = false;
            }
        }

        if (null == userInfo) {
            return AjaxResult.errorResult("用户信息不存在");
        }

        if (updateOpenId) {
            String filed = "wxGzhOpenId";
            if (ValidateUtil.isNotNull(appFlag)) {
                filed = "wxGzhOpenId2";
            }
            //获取到用户的openId后存到用户信息
            StringBuffer sb = new StringBuffer();
            sb.append("update ").append(PojoTables.UserInfo).append(" set ").append(filed).append("='").append(openId).append("',updateUser='admin_wx_response' where username = '").append(userInfo.getUsername()).append("' and delFlag =0");
            try {
                UserDaoSql.getInstance().executeBySql(sb.toString());
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("保存用户的微信openId失败，请稍后再试");
            }
            UserInfoMemory.getInstance().modifyUserInfo(username);
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.USER_INFO_MODIFY).setStr01(username);//o2o.business.jms  MQDataBusiness
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
            if (ValidateUtil.isNull(openId)) {
                return AjaxResult.errorResult("微信openId不能为空");
            }
        }

        return AjaxResult.successResult(openId);
////        Map<String, String> map = new HashMap<>();
////        map.put("code", code);
////        map.put("state", state);
//        //处理下一步
//        System.out.println("微信code回调--code:" + code + ",state:" + state);
//
//        String orderNo = state;
//
////        //boss数据通
////        String appId = "wx0a3fc42a0de29fa2";
////        String appSecret = "7c72514c9aab0e8d701189b8698a5d22";
//
//        if (ValidateUtil.isNull(orderNo)) {
//            return AjaxResult.errorResult("获取code失败");
//        }
//
//        //获取openid
//        String openId = WeixinUtil.getUserOpenID(WeixinConfig.gzhAppId, WeixinConfig.gzhAppSecret, code);
//
//        System.out.println(state + "：用户的openId为：" + openId);
//
//
//        OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
//        if (null == orderInfo) {
//            return AjaxResult.errorResult("订单不存在");
//        }
//        if (ValidateUtil.isAbsInteger(orderInfo.getIsPaid()) && 0 == orderInfo.getIsPaid() - 1) {//已经支付过了
//            return AjaxResult.errorResult("该订单已经支付过了，无需再次支付");
//        }
//
//
//        //查询店铺是否已支付成功，如果已支付成功，则不需要再支付
//        boolean hasPay = OrderPay.hasPaySuccess(ChannelUtils.wx, orderNo, "");
//        if (hasPay) {
//            return AjaxResult.errorResult("您已支付成功无需重复支付");
//        }
//
//        String xml = null;
//        try {
//            xml = WeixinPayUtils.getInstance().winxin_pay_gzh(openId, orderNo, (float) orderInfo.getTotalPrice(), 30, "商品", "商品详情");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (RepeatPayExcetion repeatPayExcetion) {
//            repeatPayExcetion.printStackTrace();
//        }
//        return xml;
//
////        return AjaxResult.successResult(map);
    }







}
